home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / modules / notifications.js < prev    next >
Text File  |  2008-07-11  |  6KB  |  155 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks Sync.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2007
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Myk Melez <myk@mozilla.org>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. const EXPORTED_SYMBOLS = ["Notifications", "Notification", "NotificationButton",
  38.                           "TabsNotification"];
  39.  
  40. const Cc = Components.classes;
  41. const Ci = Components.interfaces;
  42. const Cr = Components.results;
  43. const Cu = Components.utils;
  44.  
  45. Cu.import("resource://weave/Observers.js");
  46. Cu.import("resource://weave/log4moz.js");
  47. Cu.import("resource://weave/util.js");
  48.  
  49. let Notifications = {
  50.   // Match the referenced values in toolkit/content/widgets/notification.xml.
  51.   get PRIORITY_INFO()     1, // PRIORITY_INFO_LOW
  52.   get PRIORITY_WARNING()  4, // PRIORITY_WARNING_LOW
  53.   get PRIORITY_ERROR()    7, // PRIORITY_CRITICAL_LOW
  54.  
  55.   // FIXME: instead of making this public, dress the Notifications object
  56.   // to behave like an iterator (using generators?) and have callers access
  57.   // this array through the Notifications object.
  58.   notifications: [],
  59.  
  60.   _observers: [],
  61.  
  62.   // XXX Should we have a helper method for adding a simple notification?
  63.   // I.e. something like |function notify(title, description, priority)|.
  64.  
  65.   add: function Notifications_add(notification) {
  66.     this.notifications.push(notification);
  67.     Observers.notify(notification, "weave:notification:added", null);
  68.   },
  69.  
  70.   remove: function Notifications_remove(notification) {
  71.     let index = this.notifications.indexOf(notification);
  72.  
  73.     if (index != -1) {
  74.       this.notifications.splice(index, 1);
  75.       Observers.notify(notification, "weave:notification:removed", null);
  76.     }
  77.   },
  78.  
  79.   /**
  80.    * Replace an existing notification.
  81.    */
  82.   replace: function Notifications_replace(oldNotification, newNotification) {
  83.     let index = this.notifications.indexOf(oldNotification);
  84.  
  85.     if (index != -1)
  86.       this.notifications.splice(index, 1, newNotification);
  87.     else {
  88.       this.notifications.push(newNotification);
  89.       // XXX Should we throw because we didn't find the existing notification?
  90.       // XXX Should we notify observers about weave:notification:added?
  91.     }
  92.  
  93.     // XXX Should we notify observers about weave:notification:replaced?
  94.   }
  95.  
  96. };
  97.  
  98.  
  99. /**
  100.  * A basic notification.  Subclass this to create more complex notifications.
  101.  */
  102. function Notification(title, description, iconURL, priority, buttons) {
  103.   this.title = title;
  104.   this.description = description;
  105.  
  106.   if (iconURL)
  107.     this.iconURL = iconURL;
  108.  
  109.   if (priority)
  110.     this.priority = priority;
  111.  
  112.   if (buttons)
  113.     this.buttons = buttons;
  114. }
  115.  
  116. // We set each prototype property individually instead of redefining
  117. // the entire prototype to avoid blowing away existing properties
  118. // of the prototype like the the "constructor" property, which we use
  119. // to bind notification objects to their XBL representations.
  120. Notification.prototype.priority = Notifications.PRIORITY_INFO;
  121. Notification.prototype.iconURL = null;
  122. Notification.prototype.buttons = [];
  123.  
  124. /**
  125.  * A button to display in a notification.
  126.  */
  127. function NotificationButton(label, accessKey, callback) {
  128.   function callbackWrapper() {
  129.     try {
  130.       callback.apply(this, arguments);
  131.     } catch (e) {
  132.       let logger = Log4Moz.Service.getLogger("Notifications");
  133.       logger.error("An exception occurred: " + Utils.exceptionStr(e));
  134.       logger.info(Utils.stackTrace(e));
  135.       throw e;
  136.     }
  137.   }
  138.  
  139.   this.label = label;
  140.   this.accessKey = accessKey;
  141.   this.callback = callbackWrapper;
  142. }
  143.  
  144. function TabsNotification() {
  145.   // Call the base class's constructor to initialize the new instance.
  146.   // XXX Can we simply pass null, null for the title, description?
  147.   Notification.call(this, "", "", null, Notifications.PRIORITY_INFO, null);
  148. }
  149.  
  150. // We set each prototype property individually instead of redefining
  151. // the entire prototype to avoid blowing away existing properties
  152. // of the prototype like the the "constructor" property, which we use
  153. // to bind notification objects to their XBL representations.
  154. TabsNotification.prototype.__proto__ = Notification.prototype;
  155.